package com.app.business.web.business;

import com.app.business.service.*;
import com.app.system.log4j2.LogUtils;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.model.UserInfo;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import freemarker.template.Configuration;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 统计查询专用
 * Created by DDZ on 2020/8/26.
 *
 * @author ddz
 */
@Controller
@RequestMapping("/business/statistics/statisticsForYear/*")
@SuppressWarnings("all")
public class StatisticsForYearController {

    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private StatisticsForYearService statisticsForYearService;
    private Logger logger = LogUtils.getBusinessLogger();
    @Autowired
    private Configuration freemarkerConfiguration; // 注入bean，这里是属性注入，推荐构造注入

    /**
     * 全区汇总根据年份
     * 选房情况汇总表
     */
    @RequestMapping("chooseHouseForYearStatustics")
    @RequiresPermissions("statisticsForYear:chooseHouseForYearStatustics")
    public void chooseHouseForYearStatustics(HttpServletRequest request, ModelMap modelMap) {
        //安置项目集合
        Map<String, DataDict> townMap = dataDictService.getMapByParentNameForName("乡镇");

        UserInfo userInfo = WebUtils.getLoginUserInfo();
        String town = null;
        String townStr = null;
        String townCode = null;
        if (userInfo.getDepartment().getCode() != null && !userInfo.getDepartment().getCode().equals("001")) {
            town = userInfo.getDepartment().getName();
            townStr = town.substring(0, 2) + "片区";
            townCode = townMap.get(townStr).getValue();
        }

        List<String> yearList = this.statisticsForYearService.getChooseHouseYearList(townCode);
        List<String> townList = null;
        if (townCode != null) {
            townList = this.azProjectService.getTownList(townCode);
        } else {
            townList = this.azProjectService.getTownList();
        }
        Map<String, Map<String, Map>> resultMap = this.statisticsForYearService.getHouseStaticsForYear(townCode);
        Map<String, Map<String, Map>> storageRoomMap = this.statisticsForYearService.getStorageRoomStaticsForYear(townCode);
        Map<String, Map<String, Map>> carLotMap = this.statisticsForYearService.getCarLotStaticsForYear(townCode);
        Map<String, DataDict> townMap1 = this.dataDictService.getMapByParentNameForValue("乡镇");

        modelMap.addAttribute("townMap", townMap1);
        modelMap.addAttribute("yearList", yearList);
        modelMap.addAttribute("townList", townList);
        modelMap.addAttribute("resultMap", resultMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("carLotMap", carLotMap);

    }


    /**
     * 某镇某年
     * 选房情况汇总表
     * 每个安置项目具体情况
     */
    @RequestMapping("chooseHouseForYearAndTownStatustics")
    @RequiresPermissions("statisticsForYear:chooseHouseForYearAndTownStatustics")
    public void chooseHouseForYearAndTownStatustics(HttpServletRequest request, ModelMap modelMap) {
        //安置项目集合
        String townCode = ParamUtils.getString(request, "townCode", "");
        if (townCode == null || townCode.equals("")) {
            Map<String, DataDict> townMap = dataDictService.getMapByParentNameForName("乡镇");

            UserInfo userInfo = WebUtils.getLoginUserInfo();
            String town = null;
            String townStr = null;
            if (userInfo.getDepartment().getCode() != null && !userInfo.getDepartment().getCode().equals("001")) {
                town = userInfo.getDepartment().getName();
                townStr = town.substring(0, 2) + "片区";
                townCode = townMap.get(townStr).getValue();
            }
        }
        String yearStr = ParamUtils.getString(request, "yearStr", "");

        List<String> yearList = this.statisticsForYearService.getChooseHouseYearList(townCode);
        List<String> townList = null;
        if (townCode != null) {
            townList = this.azProjectService.getTownList(townCode);
        } else {
            townList = this.azProjectService.getTownList();
        }
        Map<String, Map<String, Map>> resultMap = this.statisticsForYearService.getHouseStaticsForYearAndTown(townCode, yearStr);
        Map<String, Map<String, Map>> storageRoomMap = this.statisticsForYearService.getStorageRoomStaticsForYearAndTown(townCode, yearStr);
        Map<String, Map<String, Map>> carLotMap = this.statisticsForYearService.getCarLotStaticsForYearAndTown(townCode, yearStr);

        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        modelMap.addAttribute("townMap", townMap);
        modelMap.addAttribute("yearList", yearList);
        modelMap.addAttribute("townList", townList);
        modelMap.addAttribute("resultMap", resultMap);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
        modelMap.addAttribute("carLotMap", carLotMap);

    }
}
